package ui;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.gui.GUIContext;
import org.newdawn.slick.gui.TextField;

import framework.BasicActionGetterInstance;
import framework.C;
import framework.states.InputConfigurator;

public class TextFieldKeyConfig extends TextField {
	
	private KeyBinding binding;
	private InputConfigurator inputConfigurator;

	public TextFieldKeyConfig(GUIContext container, Font font, int x, int y,
			int width, int height) {
		super(container, font, x, y, width, height);
		setBackgroundColor(Color.blue);
		setBorderColor(Color.white);
		binding = new KeyBinding();
	}

	public boolean isInBound(int x, int y) {
	    return	x >= getX() && x <= getX() + getWidth() 
	    		&& y >= getY() && y <= getY() + getHeight();
	}
	
	@Override
	public void render(GUIContext container, Graphics g) {
		setBackgroundColor(Color.blue);
		setBorderColor(Color.white);
		if (hasFocus()) {
			setText("Select a key...");
			setBackgroundColor(Color.red);
		} else {
			setText(toString());
		}
		super.render(container, g);
	}
	
	public KeyBinding getBinding() {
		return binding;
	}

	public void setBinding(KeyBinding binding) {
		this.binding = binding;
	}

	public void updateProperties() {
		Properties prop = BasicActionGetterInstance.getInstance().getProperties();
		FileOutputStream output;
		try {			
            File file = new File("resources/controls.properties");
            prop.put(binding.getKey(), binding.getValue());
			output = new FileOutputStream(file);
			prop.store(output, "");
			output.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		BasicActionGetterInstance.getInstance().loadKeys();
		notifyChange(inputConfigurator);
	}
	
	@Override
	public void mouseClicked(int button, int x, int y, int clickCount) {
		super.mouseClicked(button, x, y, clickCount);
		if (isInBound(x, y)) {
			//setBackgroundColor(Color.red);
			//setFocus(true);
		}
	}
	
	@Override
	public void controllerButtonPressed(int controller, int button) {
		if (hasFocus()) {	
		    binding.setValue("c" + (controller == 0 ? 1 : 2) + "" + C.CONTROL_SEPARATOR + button);
		    updateProperties();
		    setFocus(false);
		}
	}

	@Override
	public void controllerDownPressed(int controller) {
		if (hasFocus()) {	
		    binding.setValue("c" + (controller == 0 ? 1 : 2) + "" + C.CONTROL_SEPARATOR + "down");
		    updateProperties();
		    setFocus(false);
		}
	}

	@Override
	public void controllerLeftPressed(int controller) {
		if (hasFocus()) {	
		    binding.setValue("c" + (controller == 0 ? 1 : 2) + "" + C.CONTROL_SEPARATOR + "left");
		    updateProperties();
		    setFocus(false);
		}
	}

	@Override
	public void controllerRightPressed(int controller) {
		if (hasFocus()) {	
		    binding.setValue("c" + (controller == 0 ? 1 : 2) + "" + C.CONTROL_SEPARATOR + "right");
		    updateProperties();
		    setFocus(false);
		}
	}

	@Override
	public void controllerUpPressed(int controller) {
		if (hasFocus()) {	
		    binding.setValue("c" + (controller == 0 ? 1 : 2) + "" + C.CONTROL_SEPARATOR + "up");
		    updateProperties();
		    setFocus(false);
		}
	}

	@Override
	public void keyPressed(int i, char c) {
		String input = "" + c;
		if (i == Input.KEY_LEFT) {
			input = "left";
		} else if (i == Input.KEY_RIGHT) {
			input = "right";
		} else if (i == Input.KEY_UP) {
			input = "up";
		}
		if (hasFocus()) {	
		    binding.setValue("k" + "" + C.CONTROL_SEPARATOR + input);
		    updateProperties();
		    setFocus(false);
		}
	}
	
	@Override
	public String toString() {
		String[] split = binding.getValue().split(C.CONTROL_SEPARATOR);
		String result = "";
		if (split[0].length() > 0) {
			result = split[0].substring(0, 1).equals("c") ? "Controller " + split[0].substring(1, 2) : "Keyboard";			
		}
		return split.length > 1 ? result + " : " + split[1] : "";
	}
	
	public void notifyChange(InputConfigurator ic) {
		if (ic != null) {
			ic.textFieldHasChanged(this);
		}
	}
	
	public void setInputConfigurator(InputConfigurator ic) {
	    this.inputConfigurator = ic;
	}
}
